<template>
  <div>
    <!-- 标识栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item >知识图谱系统</el-breadcrumb-item>
      <el-breadcrumb-item>数据管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 添加按钮 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="primary" @click="addCateDialog = true">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 分类列表 -->
      <el-table :data="categoriesList" border stripe max-height="519px">
        <el-table-column type="expand">
          <template #default="scope">
            <p>{{scope.row.classContent}}</p>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="className" label="分类名称"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditCateDialog(scope.row)">
              </el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteCateById(scope.row.id)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加知识对话框 -->
    <el-dialog title="添加知识分类" :visible.sync="addCateDialog" width="50%" @close="addCateDialogClose()" :close-on-click-modal="false">
      <el-form :model="addCateForm" label-width="140px">
        <el-form-item label="分类名称">
          <el-input v-model="addCateForm.className"></el-input>
        </el-form-item>
        <el-form-item label="分类内容">
          <el-input v-model="addCateForm.classContent" type="textarea" :rows="3"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addCateDialog = false">取 消</el-button>
          <el-button type="primary" @click="addCate ()">确 定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 修改分类对话框 -->
    <el-dialog title="修改知识分类" :visible.sync="editCateDialog" width="50%" :close-on-click-modal="false">
      <el-form :model="editCateForm" label-width="140px">
        <el-form-item label="分类名称">
          <el-input v-model="editCateForm.className"></el-input>
        </el-form-item>
        <el-form-item label="分类内容">
          <el-input v-model="editCateForm.classContent" type="textarea" :rows="3"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editCateDialog = false">取 消</el-button>
          <el-button type="primary" @click="editCate ()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoies, updateCate, deleteCate, insertCate } from '@/assets/api.js'
export default {
  name: 'CategoriesManager',
  data () {
    return {
      // 知识分类列表
      categoriesList: [],
      // 修改知识分类对话框是否显示
      editCateDialog: false,
      // 修改知识分类表单数据
      editCateForm: {
        id: '',
        className: '',
        classContent: ''
      },
      // 添加知识分类对话框是否显示
      addCateDialog: false,
      // 添加知识分类表单数据
      addCateForm: {
        className: '',
        classContent: ''
      }
    }
  },
  methods: {
    // 获取知识分类列表
    async getCategoriesList () {
      const {data: res} = await getCategoies()
      if (res.code === 200) {
        this.categoriesList = res.data
      }
    },
    // 显示修改知识分类对话框
    showEditCateDialog (cate) {
      this.editCateForm.id = cate.id
      this.editCateForm.className = cate.className
      this.editCateForm.classContent = cate.classContent
      this.editCateDialog = true
    },
    // 执行修改知识分类操作
    async editCate () {
      const {data: res} = await updateCate(this.editCateForm)
      if (res.code === 200) {
        this.$message.success(res.msg)
        this.getCategoriesList()
      } else {
        this.$message.error(res.msg)
      }
      this.editCateDialog = false
    },
    // 执行根据id删除知识分类操作
    deleteCateById (cateId) {
      this.$confirm('此操作将永久删除该知识节点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const {data: res} = await deleteCate(cateId)
        if (res.code === 200) {
          this.getCategoriesList()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加知识分类对话栏关闭重置添加知识分类表单
    addCateDialogClose () {
      this.addCateForm.className = ''
      this.addCateForm.classContent = ''
    },
    // 执行添加知识分类操作
    async addCate () {
      const {data: res} = await insertCate(this.addCateForm)
      if (res.code === 200) {
        this.getCategoriesList()
        this.$message.success(res.msg)
      } else {
        this.$message.error(res.msg)
      }
      this.addCateDialog = false
    }
  },
  created () {
    this.getCategoriesList()
  }
}
</script>

<style scoped>

</style>
